package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.*;
import DAO.*;

import service.*;

/**
 * 主人管理Servlet，处理商品删除、增加、修改、新增等操作
 * @author Administrator
 *
 */
public class MasterServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MasterServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取前台参数，根据参数选择对应的servlet方法进行业务处理
		String op = request.getParameter("op");
		
		if("update".equals(op)){// 更新商品
			updateService(request,response);
		}else if("del".equals(op)){//删除商品
			deleteService(request,response);
		}else if("insert".equals(op)){//添加商品
			insertService(request,response);
		}else if("select".equals(op)){//查询商品
			selectService(request,response);
		}
		
		
	}
	/**
	 * 	宠物信息
	 * @param response
	 */
	private void selectService(HttpServletRequest request,
			HttpServletResponse response) {
		
		IMasterDao dao = DaoFactory.getMasterDao();
		  Map<String,Object> map = null;

		  try{
		  if(dao.queryMaster(map)!=null){
			  request.getRequestDispatcher("客户信息.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("main1.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}
	/**
	 * 增加宠物
	 * @param request
	 * @param response
	 */
	private void insertService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//获取前台传递的三个参数，其中id在数据中使用自增字段，无需手动输入
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		int pet_id=Integer.parseInt(request.getParameter("pet_id"));
		int tel=Integer.parseInt(request.getParameter("tel"));
		String adress=request.getParameter("adress");	
        Masterbean p=new Masterbean(id, name, pet_id, tel, adress);
		IMasterDao dao = DaoFactory.getMasterDao();
		try{
		if(dao.insert(p)){
			request.getRequestDispatcher("客户信息.jsp").forward(request, response);
		}else{
			response.sendRedirect("增加客户.jsp");
		}}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 删除宠物
	 * @param request
	 * @param response
	 */
	private void deleteService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//接收前台传递的待删除商品ID，根据id删除对应商品
		//delete from product where id ="id";
		int id =Integer.parseInt(request.getParameter("id"));
		Masterbean p = new Masterbean();
		p.setId(id);
		IMasterDao dao = DaoFactory.getMasterDao();
		try{
		if(dao.delete(p)){
			request.getRequestDispatcher("客户信息.jsp").forward(request, response);
			
		}else{
			response.sendRedirect("客户信息.jsp");
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 更新宠物
	 * @param request
	 * @param response
	 */
	private void updateService(HttpServletRequest request,
			HttpServletResponse response) {
		//获取前台更新商品的参数，根据商品id进行更新
		//update product set 
		//product.name ="xxx",product.addr="xxx",product.price="xxx" 
		//where id ="待删除商品id";
		
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		int pet_id=Integer.parseInt(request.getParameter("pet_id"));
		int tel=Integer.parseInt(request.getParameter("tel"));
		String adress=request.getParameter("adress");	
        Masterbean p=new Masterbean(id, name, pet_id, tel, adress);
		IMasterDao dao = DaoFactory.getMasterDao();
		  try{
		  if(dao.update(p)){
			  request.getRequestDispatcher("客户信息.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("更新客户.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}

	public void init() throws ServletException {
	}

}
